#!/usr/bin/env bash

export PATH="${PATH}:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

set -e

ME="$(basename ${0})"


# -----------------------------------------------------------------------------
# show what is happening

echo >&2 "${ME} on '${NODENAME}' (${NODEID}), interface '${IFNAME}' (type '${IFTYPE}', subtype '${IFSUBTYPE}', mac '${MAC}', mtu '${MTU}', ifupdata '${IFUPDATA}'), config '${CONFBASE}', nodes ${NODES}"


# -----------------------------------------------------------------------------
# functions

run() {
    printf >&2 " > "
    printf >&2 "%q " "${@}"
    printf >&2 "\n"
    "${@}"
    return $?
}

# -----------------------------------------------------------------------------
# parse the IP and NET from ${IFUPDATA}

NET="$(echo "${IFUPDATA}" | cut -d '|' -f 1)"
IP="$(echo "${IFUPDATA}" | cut -d '|' -f 2)"
echo >&2 "IP       : ${IP}"
echo >&2 "NET      : ${NET}"

# -----------------------------------------------------------------------------
# bring the interface up

os="$(uname -s)"
case "${os}" in
	Linux)
		run ip link set ${IFNAME} address ${MAC} mtu ${MTU} up
		run ip addr add ${IP} dev ${IFNAME}
		run ip route replace ${NET} dev ${IFNAME} || \
			run ip route add ${NET} dev ${IFNAME}
		;;

	FreeBSD)
		run ifconfig ${IFNAME} inet ${IP}/${NET/*\/} up description "GVPE tunnel"
		;;

	*)
		echo >&2 "Unknown operating system '${os}'. Configuration may be incomplete."
		;;
esac

# -----------------------------------------------------------------------------
# we just connected, mark all nodes down

cd ${CONFBASE}/status
active_nodes=0
while read x
do
	active_nodes=$((active_nodes + 1))

	[ "${x}" = "${MYNODENAME}" ] && continue
	[ ! -f "${x}" ] && echo >&2 "File '${x}' missing!" && continue

	source "${x}.reset"
	#eval "mac=\${MAC_${nodeid}-${mac}}; ifupdata=\${IFUPDATA_${nodeid}-${ifupdata}}"
	eval "mac=\${MAC_${nodeid}-${mac}}"
	cat >${x} <<EOF
nodeid="${nodeid}"
name="${name}"
status="down"
ip="${ip}"
si="${si}"
pip="${pip}"
pipport="${pipport}"
rip="${rip}"
ripport="${ripport}"
mac="${mac}"
ifupdata="${ifupdata}"
timestamp="$(date +%s)"
EOF
done <nodes

# set our node to up
source ${CONFBASE}/status/${NODENAME}
cat >${CONFBASE}/status/${NODENAME} <<EOF
nodeid="${NODEID}"
name="${NODENAME}"
status="up"
ip="${IP}"
si="${si}"
pip="${pip}"
pipport="${pipport}"
rip="${rip}"
ripport="${ripport}"
mac="${MAC}"
ifupdata="${IFUPDATA}"
timestamp="$(date +%s)"
EOF

# -----------------------------------------------------------------------------
# initialize the counters

EVENTS=0
NODES_UP=1
NODES_DOWN=$((NODES - 1))
cat >${CONFBASE}/status/status <<EOF
MYNODENAME=${NODENAME}
MYNODEID=${NODEID}
EVENTS=$((EVENTS + 1))
NODES_ALL=${NODES}
NODES_UP=${NODES_UP}
NODES_DOWN=${NODES_DOWN}
NODES_DISABLED=$((NODES - active_nodes))
timestamp="$(date +%s)"
EOF

# -----------------------------------------------------------------------------
# execute any local action

if [ -x ${CONFBASE}/${ME}.local ]
	then
	run ${CONFBASE}/${ME}.local "${@}"
	exit $?
fi

exit 0
